
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
import java.lang.Thread;

import javax.swing.*;

import com.speljohan.rsbot.script.*;
import com.speljohan.rsbot.script.wrappers.*;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.*;
import com.speljohan.rsbot.accessors.Player;
import com.speljohan.rsbot.bot.Bot;

public class AzWFletcher extends Script implements PaintListener, ServerMessageListener {

        public int logID = 0;
        public int bowID = 0;
        public int knifeID = 946;
        public int xPos = 0;
        public int yPos = 0;
        public int fletched = 0;
        public int requiredLevel = 0;
        public int numbOfLogs = 0;
        public int numbOfBows = 0;
        public int numbOfStrings = 0;
        public int done = 0; // for animation purposes
        public int currentLevel = skills.getCurrentSkillLevel(9);
        public int currentEXP = skills.getCurrentSkillExp(9);
        public long currentTime = System.currentTimeMillis();
        public String bowName = "";
        public String logName = "";
        public int interfaceID = 304;
        public boolean withdrawing = false;
        public boolean onStart = true;
        public boolean usingPC = false;
        public boolean stringing = false;

        public boolean onStart(Map<String, String> args) {
           

                logName = args.get("log");
                bowName = args.get("fletch");
                if (args.get("string") != null) {
                        stringing = true;
                }

                // i'll just add the stringing and break the loop..
                // cbb touching the original code
                if (stringing) {
                        knifeID = 1777; // using that var for strings
                        xPos = 255;
                        yPos = 415;
                        interfaceID = 309;

                        if (logName.contains("Normal")) {
                                if (bowName.contains("Longbow")) {
                                        logID = 48;
                                        requiredLevel = 10;
                                }
                                else if (bowName.contains("Shortbow")) {
                                        logID = 50;
                                        requiredLevel = 5;
                                }
                                else {
                                        log("The combination you chose does not exist.");
                                        log("You chose: " + logName + " and: " + bowName + ".");
                                        stopAllScripts();
                                }
                        }
                        else if (logName.contains("Oak")) {
                                if (bowName.contains("Longbow")) {
                                        logID = 56;
                                        requiredLevel = 25;
                                }
                                else if (bowName.contains("Shortbow")) {
                                        logID = 54;
                                        requiredLevel = 20;
                                }
                                else {
                                        log("The combination you chose does not exist.");
                                        log("You chose: " + logName + " and: " + bowName + ".");
                                        stopAllScripts();
                                }
                        }
                        else if (logName.contains("Willow")) {
                                if (bowName.contains("Longbow")) {
                                        logID = 58;
                                        requiredLevel = 40;
                                }
                                else if (bowName.contains("Shortbow")) {
                                        logID = 60;
                                        requiredLevel = 35;
                                }
                                else {
                                        log("The combination you chose does not exist.");
                                        log("You chose: " + logName + " and: " + bowName + ".");
                                        stopAllScripts();
                                }
                        }
                        else if (logName.contains("Maple")) {
                                if (bowName.contains("Longbow")) {
                                        logID = 62;
                                        requiredLevel = 55;
                                }
                                else if (bowName.contains("Shortbow")) {
                                        logID = 64;
                                        requiredLevel = 50;
                                }
                                else {
                                        log("The combination you chose does not exist.");
                                        log("You chose: " + logName + " and: " + bowName + ".");
                                        stopAllScripts();
                                }
                        }
                        else if (logName.contains("Yew")) {
                                if (bowName.contains("Longbow")) {
                                        logID = 66;
                                        requiredLevel = 70;
                                }
                                else if (bowName.contains("Shortbow")) {
                                        logID = 68;
                                        requiredLevel = 65;
                                }
                                else {
                                        log("The combination you chose does not exist.");
                                        log("You chose: " + logName + " and: " + bowName + ".");
                                        stopAllScripts();
                                }
                        }
                        else if (logName.contains("Magic")) {
                                if (bowName.contains("Longbow")) {
                                        logID = 70;
                                        requiredLevel = 85;
                                }
                                else if (bowName.contains("Shortbow")) {
                                        logID = 72;
                                        requiredLevel = 80;
                                }
                                else {
                                        log("The combination you chose does not exist.");
                                        log("You chose: " + logName + " and: " + bowName + ".");
                                        stopAllScripts();
                                }
                        }
                }
                else {

                        if (logName.contains("Normal")) {
                                logID = 1511;
                                interfaceID = 305;

                                if (bowName.contains("Short")) {
                                        bowID = 50;
                                        xPos = 200;
                                        yPos = 390;
                                        requiredLevel = 5;
                                }
                                else if (bowName.contains("Long")) {
                                        bowID = 48;
                                        xPos = 320;
                                        yPos = 390;
                                        requiredLevel = 10;
                                }
                                else if (bowName.contains("Shaft")) {
                                        bowID = 52;
                                        xPos = 80;
                                        yPos = 390;
                                        requiredLevel = 1;
                                }
                                else {
                                        bowID = 1;
                                        xPos = 415;
                                        yPos = 390;
                                        requiredLevel = 9;
                                }
                        }
                        else {
                                if (bowName.contains("Short")) {
                                        xPos = 115;
                                        yPos = 390;
                                }
                                else if (bowName.contains("Long")) {
                                        xPos = 260;
                                        yPos = 390;
                                }
                                else {
                                        log("The combination you chose does not exist.");
                                        log("You chose: " + logName + " and: " + bowName + ".");
                                        stopAllScripts();
                                }

                                if (logName.contains("Oak")) {
                                        logID = 1521;

                                        if (bowName.contains("Short")) {
                                                bowID = 54;
                                                requiredLevel = 20;
                                        }
                                        else if (bowName.contains("Long")) {
                                                bowID = 56;
                                                requiredLevel = 25;
                                        }
                                }
                                else if (logName.contains("Willow")) {
                                        logID = 1519;

                                        if (bowName.contains("Short")) {
                                                bowID = 60;
                                                requiredLevel = 35;
                                        }
                                        else if (bowName.contains("Long")) {
                                                bowID = 58;
                                                requiredLevel = 40;
                                        }
                                }
                                else if (logName.contains("Maple")) {
                                        logID = 1517;

                                        if (bowName.contains("Short")) {
                                                bowID = 64;
                                                requiredLevel = 50;
                                        }
                                        else if (bowName.contains("Long")) {
                                                bowID = 62;
                                                requiredLevel = 55;
                                        }
                                }
                                else if (logName.contains("Yew")) {
                                        logID = 1515;

                                        if (bowName.contains("Short")) {
                                                bowID = 68;
                                                requiredLevel = 65;
                                        }
                                        else if (bowName.contains("Long")) {
                                                bowID = 66;
                                                requiredLevel = 70;
                                        }
                                }
                                else if (logName.contains("Magic")) {
                                        logID = 1513;

                                        if (bowName.contains("Short")) {
                                                bowID = 72;
                                                requiredLevel = 80;
                                        }
                                        else if (bowName.contains("Long")) {
                                                bowID = 70;
                                                requiredLevel = 85;
                                        }
                                }
                        }
                }

                log("You chose: " + logName + " and: " + bowName + ".");
                if (stringing) {
                        log("and we are going to be stringing these.");
                }

                return true;
        }

        public int loop() {
                if (!isLoggedIn()) {
                        return random(300, 500);
                }

                if (isLoggedIn() && onStart) {
                        onStart = false;

                        while (currentLevel == 0 && currentEXP == 0) {
                                currentLevel = skills.getCurrentSkillLevel(9);
                                currentEXP = skills.getCurrentSkillExp(9);
                        }

                        openBank(); // now needed to do count
                        wait(500);
                        if (stringing) {
                                doDepositExcept();
                        }
                        else {
                                doDepositExcept(knifeID);
                        }
                        countBankStack();

                        log("Current level is: " + currentLevel);
                        log("Current EXP is: " + currentEXP);
                        log("Required level: " + requiredLevel);
                }

                if (isLoggedIn() && currentLevel < requiredLevel && currentLevel != 0 && currentEXP != 0) {
                        log("You do not have the required level to fletch the specified combination.");
                        log("You chose: " + logName + " and: " + bowName + ".");
                        stopAllScripts();
                }

                if (isLoggedIn() && getCurrentTab() != TAB_INVENTORY) {
                        openTab(TAB_INVENTORY);
                }

                if (isLoggedIn() && inventoryContains(logID) && inventoryContains(knifeID)) {
                        if (getCurrentTab() != TAB_INVENTORY) {
                                openTab(TAB_INVENTORY);
                        }

                        doInventory(logID, "Use");
                        wait(random(350, 500));
                        doInventory(knifeID, "Use");
                        wait(random(500, 700));

                        int fail = 0;
                        while (!interfaceExists(interfaceID)) {
                                // the dialog on the bottom does not exist
                                fail++; // better add a failsafe
                                if (fail >= 10) {
                                        fail = 0;
                                        doInventory(logID, "Use");
                                        wait(random(350, 500));
                                        doInventory(knifeID, "Use");
                                        wait(random(200, 400));
                                }
                                else {
                                        wait(random(500, 650));
                                }
                        }

                        clickMouse(xPos + random(-5, 5), yPos + random(-5, 5), false);
                        if (!stringing) {
                                atMenu("Make X");
                                wait(random(500, 700));

                                fail = 0;
                                while (!interfaceExists(752)) {
                                        // the second dialog
                                        fail++;
                                        if (fail >= 10) {
                                                return random(200, 400); // failed so let's just try again.
                                        }
                                        else {
                                                wait(random(200, 450));
                                        }
                                }
                                wait(random(500, 1000)); // just incase

                                String random = String.valueOf(random(3, 9));
                                String random2 = String.valueOf(Integer.parseInt(random) + random(-2, 2));
                                int randInt = Integer.parseInt(random + random2);

                                sendText(String.valueOf(randInt), false);
                                if (RSInterface.getChildInterface(752, 5).getText().contains(random + random2)) {
                                        sendKey((char) KeyEvent.VK_ENTER);
                                }
                                else {
                                        for (int i = 1; i < RSInterface.getChildInterface(752, 5).getText().length() - 1; i++) {
                                                sendKey((char) KeyEvent.VK_BACK_SPACE);
                                                wait(50);
                                        }

                                        sendText(String.valueOf(randInt), true);
                                }
                                wait(random(250, 500));
                                checkChatBox();
                        }
                        else {
                                atMenu("Make All");
                                wait(random(250, 400));
                        }

                        wait(random(250, 400));
                        // finally.. now for the animation check

                        waitTillAnimationStop();

                        if (!stringing && numbOfLogs <= 0) {
                                log("Completed all logs. Logging out.");
                                logout();
                                return -1; // logout and end.
                        }
                        else if (stringing && (numbOfStrings <= 0 || numbOfBows <= 0)) {
                                log("Completed all bows. Logging out.");
                                logout();
                                return -1;
                        }
                }
                else if (!inventoryContains(logID) || !inventoryContains(knifeID)) {
                        doBank();
                        return random(750, 1000);
                }

                return random(500, 1000);
        }

        public void waitTillAnimationStop() {
                int shouldAntiBan = 0;
                done = 0;
                while (true) {
                        while (getMyPlayer().getAnimation() != -1) {
                                wait(random(300, 500));
                                shouldAntiBan++;
                                if (shouldAntiBan == random(1, 10)) {
                                        AzWAntiBan();
                                        shouldAntiBan = 0;
                                }
                                else if (shouldAntiBan > 10) {
                                        shouldAntiBan = 0;
                                        AzWAntiBan();
                                }
                        }

                        if (getMyPlayer().getAnimation() == -1) {
                                // stopped, but for how long?
                                if (done >= 2 && !stringing) {
                                        break;
                                }
                                else if (done >= 4 && stringing) {
                                        break;
                                }
                                else {
                                        done++;
                                        shouldAntiBan++;
                                        wait(random(300, 500));
                                }
                        }
                        else {
                                done = 0; // guess it wasn't for long XD
                        }
                }
        }

        public void checkChatBox() {
                String whatAmISaying = RSInterface.getChildInterface(137, 51).getText();
                String noSeriously = whatAmISaying.substring(whatAmISaying.indexOf("f>"), whatAmISaying.indexOf("*"));
                noSeriously = noSeriously.replaceAll("f>", "");

                if (!noSeriously.equals("")) {
                        log("Chat box: " + noSeriously);
                        log("Backspacing...");
                        for (int i = 0; i < noSeriously.length(); i++) {
                                sendKey((char) KeyEvent.VK_BACK_SPACE);
                                wait(50);
                        }
                }
        }

        public void doInventory(int id, String action) {
                // okay, RSBot really needs an update
                int[] items = getInventoryArray(); // gets all items
                ArrayList<Integer> theItems = new ArrayList<Integer>(); // all possible matches

                for (int i = 0; i < items.length; i++) {
                        if (items[i] == id) {
                                theItems.add(i); // add all possible matches to the arraylist
                        }
                }

                if (theItems.isEmpty() || theItems == null) {
                        // ..... error LOL
                        stopAllScripts();
                }

                int randPossible = theItems.get(random(0, theItems.size())); // get a random possible match
                Point itemPos = getInventoryItemPoint(randPossible); // get the XY of the item

                // now we should interpret the action
                if (action.equals("Use")) {
                        clickMouse(itemPos, 5, 5, true); // left click with 5 xy tolerance
                }
                else {
                        clickMouse(itemPos, 5, 5, false); // right click with 5 xy tolerance
                        atMenu(action); // and then do the action
                }
        }

        public boolean doDepositExcept(int... ids) {
                // custom deposit except
                int inventoryCount = getInventoryCount();
                int[] inventoryArray = getInventoryArray();

                outer:
                     for (int off = 0; off < inventoryArray.length; off++) {
                             if (inventoryArray[off] == -1) {
                                     continue;
                             }
                             for (int item : ids) {
                                     if (inventoryArray[off] == item) {
                                             continue outer;
                                     }
                             }

                             for (int tries = 0; tries < 5; tries++) {
                                     atInventoryItem(inventoryArray[off], "Deposit-All");
                                     wait(random(800, 1200));
                                     if (getInventoryCount() < inventoryCount) {
                                             break;
                                     }
                             }
                             if (getInventoryCount() >= inventoryCount) {
                                     // equally, otherwise
                                     // something really weird
                                     // happend :P
                                     return false;
                             }
                             inventoryArray = getInventoryArray();
                             inventoryCount = getInventoryCount();
                     }
                return true;
        }

        public void openBank() {
                int random = random(1, 10);

                int[] bankObj = null;
                int[] bankNPC = null;

                while (!interfaceExists(762) && !RSInterface.getInterface(13).isValid() && !RSInterface.getInterface(14).isValid()) {
                        if (random > 5 || usingPC) {
                                if (distanceTo(new RSTile(3253, 3422)) < 10) {
                                        // Varrock East Bank
                                        bankObj = new int[]{11402};
                                }
                                else if (distanceTo(new RSTile(3186, 3438)) < 10) {
                                        // Varrock West Bank
                                        bankObj = new int[]{11402};
                                }
                                else if (distanceTo(new RSTile(3013, 3356)) < 10) {
                                        // Falador East Bank
                                        bankObj = new int[]{11758};
                                }
                                else if (distanceTo(new RSTile(2945, 3370)) < 10) {
                                        // Falador West Bank
                                        bankObj = new int[]{11758};
                                }
                                else if (distanceTo(new RSTile(3209, 3218)) < 10) {
                                        // Lumbridge Bank
                                        bankObj = new int[]{36786};
                                }
                                else if (distanceTo(new RSTile(3093, 3243)) < 10) {
                                        // Draynor Bank
                                        bankObj = new int[]{2213};
                                }
                                else if (distanceTo(new RSTile(3270, 3167)) < 10) {
                                        // Al Kharid Bank
                                        bankObj = new int[]{35647, 35678};
                                }
                                else if (distanceTo(new RSTile(2668, 2653)) < 10) {
                                        // Pest Control Bank
                                        bankObj = new int[]{14367};
                                }

                                // actually open the bank now
                                if (bankObj != null) {
                                        if (bankObj.length == 1) {
                                                RSObject object = findObject(bankObj[0]);
                                                atObject(object, "Use-quickly");
                                        }
                                        else {
                                                random = random(0, bankObj.length - 1);

                                                RSObject object = findObject(bankObj[random]);
                                                atObject(object, "Use-quickly");
                                        }
                                        wait(random(500, 1000));
                                }
                        }
                        else {
                                if (distanceTo(new RSTile(3253, 3422)) < 10) {
                                        // Varrock East Bank
                                        bankNPC = new int[]{5912, 5913};
                                }
                                else if (distanceTo(new RSTile(3186, 3438)) < 10) {
                                        // Varrock West Bank
                                        bankNPC = new int[]{5912, 5913};
                                }
                                else if (distanceTo(new RSTile(3013, 3356)) < 10) {
                                        // Falador East Bank
                                        bankNPC = new int[]{6200};
                                }
                                else if (distanceTo(new RSTile(2945, 3370)) < 10) {
                                        // Falador West Bank
                                        bankNPC = new int[]{6200};
                                }
                                else if (distanceTo(new RSTile(3209, 3218)) < 10) {
                                        // Lumbridge Bank
                                        bankNPC = new int[]{4907};
                                }
                                else if (distanceTo(new RSTile(3093, 3243)) < 10) {
                                        // Draynor Bank
                                        bankNPC = new int[]{495, 494};
                                }
                                else if (distanceTo(new RSTile(3270, 3167)) < 10) {
                                        // Al Kharid Bank
                                        bankNPC = new int[]{496, 497};
                                }
                                else if (distanceTo(new RSTile(2668, 2653)) < 10) {
                                        // Pest Control Bank
                                        usingPC = true;
                                }

                                // actually open the bank now
                                if (bankNPC != null && !usingPC) {
                                        if (bankNPC.length == 1) {
                                                RSNPC npc = getNearestNPCByID(bankNPC[0]);
                                                atNPC(npc, "Bank Bank");
                                        }
                                        else {
                                                random = random(0, bankNPC.length - 1);

                                                RSNPC npc = getNearestNPCByID(bankNPC[random]);
                                                atNPC(npc, "Bank Bank");
                                        }
                                        wait(random(500, 1000));
                                }
                        }
                }

                if (RSInterface.getInterface(13).isValid() || RSInterface.getInterface(14).isValid()) {
                        // doesn't pick up bank pins? --- copy of BankPasswords.java
                        if (RSInterface.getInterface(14).isValid()) {
                                atInterface(RSInterface.getInterface(14).getChild(1));
                                wait(random(800, 1500));
                        }

                        String pin = getAccountProperty("pin");

                        if (pin == null || pin == "") {
                                log("You must add a new key in account manager called 'PIN' with the value of your PIN.");
                                log("Could not enter PIN. Stopping script.");
                                wait(20000);
                                stopAllScripts();
                        }
                        else {
                                log("Entering bank PIN.");
                        }

                        while (!enterCode(pin)) {
                                // wait(random(300,1000));
                        }

                        // Click to continue
                        if (RSInterface.getInterface(211).isValid()) {
                                atInterface(RSInterface.getInterface(211).getChild(3));
                                wait(random(100, 1500));
                        }
                }
        }

        public boolean enterCode(String pass) {
                if (!RSInterface.getInterface(13).isValid()) {
                        return true;
                }
                RSInterface pif = RSInterface.getInterface(13);
                RSInterfaceChild[] children = pif.getChildren();
                int state = 0;
                for (int i = 21; i < 25; i++) {
                        if (children[i].containsText("?")) {
                                state++;
                        }
                }
                state = 4 - state;

                for (int j = 11; j < 21; j++) {
                        if (children[j].containsText(pass.substring(state, state + 1))) {
                                atInterface(children[j - 10]);
                                wait(random(1000, 2000));
                                break;
                        }
                        else {
                                if (j == 20) {
                                        moveMouse(random(0, 750), random(0, 500));
                                }
                        }
                }

                if (state == 3 || !RSInterface.getInterface(13).isValid()) {
                        return true;
                }
                return false;
        }

        public void doBank() {
                if (!interfaceExists(762) && !RSInterface.getInterface(13).isValid() && !RSInterface.getInterface(14).isValid()) {
                        openBank();
                        wait(random(400, 600));
                }

                if (interfaceExists(762) || RSInterface.getInterface(13).isValid() || RSInterface.getInterface(14).isValid()) {
                        doDepositExcept(knifeID, logID);
                        if (!isInventoryFull()) {
                                if (fletched == 0 && numbOfBows == 0 && numbOfStrings == 0 && numbOfLogs == 0) {
                                        countBankStack();
                                }

                                if (!inventoryContains(knifeID) && !stringing) {
                                        bank.withdraw(knifeID, 1);
                                }
                                if (!isInventoryFull()) {
                                        if (!stringing) {
                                                int randX = random(130, 150);
                                                int randY = random(95, 120);

                                                clickMouse(randX, randY, false);
                                                wait(random(500, 700));
                                                atMenu("Withdraw-All");
                                        }
                                        else {
                                                int randBowX = random(130, 150);
                                                int randBowY = random(95, 120);

                                                int randStringX = random(175, 195);
                                                int randStringY = random(95, 120);

                                                moveMouse(randBowX, randBowY);
                                                wait(random(500, 700));
                                                if (!getMenuItems().contains("Withdraw-14")) {
                                                        clickMouse(false);
                                                        atMenu("Withdraw-X");
                                                        wait(random(800, 1000));

                                                        sendText("14", true); // has to be 14
                                                        wait(random(700, 1000));
                                                }
                                                else {
                                                        clickMouse(false);
                                                        wait(random(500, 700));
                                                        atMenu("Withdraw-14");
                                                        wait(random(400, 600));
                                                }

                                                clickMouse(randStringX, randStringY, false);
                                                wait(random(500, 700));
                                                atMenu("Withdraw-All");
                                                wait(random(400, 600));
                                        }
                                }
                                checkChatBox();
                        }
                        else {
                                if (!inventoryContains(knifeID) && !stringing) {
                                        // bank does not work yet.
                                        log("Lost knife. Error.");
                                        stopAllScripts();
                                /*
                                openBank();
                                atInventoryItem(logID, "Deposit-1");
                                bank.withdraw(knifeID, 1);
                                 */
                                }
                        }

                        bank.close();
                }
        }

        public void countBankStack() {
                int invCount = getInventoryCount();
                // bank.setWithdrawModeToNote(); doubt it even works
                if (getSetting(115) == 0) { // note
                        clickMouse(230, 300, 20, 15, true);
                        wait(200);
                }


                if (!stringing) {
                        int randX = random(130, 150);
                        int randY = random(95, 120);

                        clickMouse(randX, randY, false);
                        wait(random(500, 700));
                        atMenu("but one");
                        while (invCount == getInventoryCount()) {
                                wait(random(200, 400));
                        }
                        numbOfLogs = getInventoryStackArray()[getInventoryCount() - 1] + 1;
                        wait(200);
                        doDepositExcept(knifeID);
                }
                else {
                        int randBowX = random(130, 150);
                        int randBowY = random(95, 120);

                        int randStringX = random(175, 195);
                        int randStringY = random(95, 120);

                        clickMouse(randBowX, randBowY, false);
                        wait(random(500, 700));
                        atMenu("but one");
                        while (invCount == getInventoryCount()) {
                                wait(random(200, 400));
                        }
                        numbOfBows = getInventoryStackArray()[getInventoryCount() - 1] + 1;

                        clickMouse(randStringX, randStringY, false);
                        wait(random(500, 700));
                        atMenu("but one");
                        while (invCount == getInventoryCount()) {
                                wait(random(200, 400));
                        }
                        wait(random(500, 750));
                        numbOfStrings = getInventoryStackArray()[getInventoryCount() - 1] + 1;
                        doDepositExcept();
                }
                wait(200);

                if (getSetting(115) == 1) { // note
                        clickMouse(230, 300, 20, 15, true);
                        wait(200);
                }
        }

        public void AzWAntiBan() {
                //antiban activated.
                int antiban = random(1, 40);
                checkChatBox();

                if (antiban < 5 && antiban > 0) {
                        // player examining
                        RSCharacter nearPlayer = getNearestPlayerByLevel(1, 130);

                        if (nearPlayer != null) {
                                if (nearPlayer.getScreenLocation().x < 10 || nearPlayer.getScreenLocation().y > 500) {
                                        log("AzWAntiBan: Nearby player is too far away.");
                                }
                                else {
                                        log("AzWAntiBan: Moving mouse to a nearby player.");
                                        moveMouse(nearPlayer.getScreenLocation().x, nearPlayer.getScreenLocation().y);
                                        int random = random(1, 10);
                                        if (random > 7) {
                                                log("AzWAntiBan: Right-clicking nearby player.");
                                                clickMouse(nearPlayer.getScreenLocation().x, nearPlayer.getScreenLocation().y, false);
                                                random = random(1, 10);
                                                if (random > 7) {
                                                        log("AzWAntiBan: Pressing cancel.");
                                                        atMenu("Cancel");
                                                }
                                        }
                                }
                        }
                }

                if (antiban < 15 && antiban > 5) {
                        //tabs
                        int[] tabs = new int[]{TAB_ATTACK, TAB_CLAN, TAB_EQUIPMENT, TAB_FRIENDS, TAB_IGNORE, TAB_INVENTORY, TAB_MAGIC, TAB_MUSIC, TAB_OPTIONS, TAB_PRAYER, TAB_QUESTS, TAB_STATS};
                        String[] tabNames = new String[]{"Combat", "Clan", "Equipment", "Friends", "Ignore", "Inventory", "Magic", "Music", "Options", "Prayer", "Quests", "Statistics"};
                        int randTab = random(0, tabs.length - 1);

                        // let's think twice before selecting a useless tab
                        int random = random(1, 10);
                        if (random > 5) {
                                // useless means: attack, clan, ignore, music, quest, options
                                if (tabNames[randTab].contains("Attack") || tabNames[randTab].contains("Clan") || tabNames[randTab].contains("Ignore") || tabNames[randTab].contains("Music") || tabNames[randTab].contains("Quest") || tabNames[randTab].contains("Options")) {
                                        AzWAntiBan();
                                }
                        }

                        log("AzWAntiBan: Selecting the " + tabNames[randTab] + " tab.");

                        if (getCurrentTab() != tabs[randTab]) {
                                openTab(tabs[randTab]);
                        }
                        else {
                                AzWAntiBan();
                        }

                        if (tabs[randTab] == TAB_STATS) {
                                log("AzWAntiBan: Checking experience points of random stat.");
                                int randX = random(555, 710);
                                int randY = random(230, 445);

                                moveMouse(randX, randY);
                                wait(random(6000, 8000));
                        }

                        if (tabs[randTab] == TAB_OPTIONS) {
                                if (isRunning()) {
                                        log("AzWAntiBan: Activating run.");
                                        setRun(true);
                                }
                        }
                }

                if (antiban < 40 && antiban > 25) {
                        //mouse -- 765, 500
                        int randX = random(0, 765);
                        int randY = random(0, 500);

                        String randomXY = String.valueOf(randX) + "," + String.valueOf(randY);

                        log("AzWAntiBan: Moving mouse to - " + randomXY);
                        moveMouse(randX, randY, 3, 3);

                        int randomExamine = random(1, 10);
                        if (randomExamine > 3) {
                                wait(500);
                                if (getMenuItems().contains("Examine")) {
                                        clickMouse(getMouseLocation().x, getMouseLocation().y, false);
                                        wait(500);
                                        if (atMenu("Examine")) {
                                                log("AzWAntiBan: Successfully examined mouse position!");
                                        }
                                }
                        }
                }

                if (antiban < 25 && antiban > 15) {
                        log("AzWAntiBan: Moving the compass / camera.");

                        //compass and camera -- thanks to WarXperiment ~ AllAntiBan
                        char[] LR = new char[]{KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT};
                        char[] UD = new char[]{KeyEvent.VK_DOWN, KeyEvent.VK_UP};
                        char[] LRUD = new char[]{KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT,
                                                 KeyEvent.VK_UP, KeyEvent.VK_UP
                        };
                        int random2 = random(0, 2);
                        int random1 = random(0, 2);
                        int random4 = random(0, 4);

                        if (random(0, 3) == 0) {
                                Bot.getInputManager().pressKey(LR[random1]);
                                try {
                                        Thread.sleep(random(100, 400));
                                } catch (Exception e) {
                                }
                                Bot.getInputManager().pressKey(UD[random2]);
                                try {
                                        Thread.sleep(random(300, 600));
                                } catch (Exception e) {
                                }
                                Bot.getInputManager().releaseKey(UD[random2]);
                                try {
                                        Thread.sleep(random(100, 400));
                                } catch (Exception e) {
                                }
                                Bot.getInputManager().releaseKey(LR[random1]);
                        }
                        else {
                                Bot.getInputManager().pressKey(LRUD[random4]);
                                if (random4 > 1) {
                                        try {
                                                Thread.sleep(random(300, 600));
                                        } catch (Exception e) {
                                        }
                                }
                                else {
                                        try {
                                                Thread.sleep(random(500, 900));
                                        } catch (Exception e) {
                                        }
                                }
                                Bot.getInputManager().releaseKey(LRUD[random4]);
                        }
                }
        }

        public void serverMessageRecieved(ServerMessageEvent svr) {
                String msg = svr.getMessage();

                if (msg.contains("carefully cut the")) {
                        fletched++;
                        numbOfLogs--;
                }
                if (msg.contains("add a string")) {
                        fletched++;
                        numbOfBows--;
                        numbOfStrings--;
                }
                if (msg.contains("run out of")) {
                        done = 10; // automatically breaks waitTillAnimationStop();
                }
        }

        public void onRepaint(Graphics g) {
                long millis = System.currentTimeMillis() - currentTime;
                long hours = millis / (1000 * 60 * 60);
                millis -= hours * 1000 * 60 * 60;
                long minutes = millis / (1000 * 60);
                millis -= minutes * 1000 * 60;
                long seconds = millis / 1000;

                StringBuilder b = new StringBuilder();
                b.append(hours);
                b.append(':');
                if (minutes < 10) {
                        b.append('0');
                }
                b.append(minutes);
                b.append(':');
                if (seconds < 10) {
                        b.append('0');
                }
                b.append(seconds);

                String timeElapsed = b.toString();

                g.setColor(Color.MAGENTA);

                g.drawString("AzWFletcher v" + getVersion(), 25, 235);
                g.drawString("Gained Levels: " + (skills.getCurrentSkillLevel(9) - currentLevel), 25, 250);
                g.drawString("Gained EXP: " + (skills.getCurrentSkillExp(9) - currentEXP), 25, 265);
                g.drawString("EXP till next level: " + skills.getXPToNextLevel(9), 25, 280);
                g.drawString("Time elapsed: " + timeElapsed, 25, 295);
                if (!stringing) {
                        g.drawString("Logs left: " + numbOfLogs, 25, 310);
                        g.drawString("Fletched: " + fletched, 25, 325);
                }
                else {
                        g.drawString("Strings left: " + numbOfStrings + " || " + "Bows left: " + numbOfBows, 25, 310);
                        g.drawString("Strung: " + fletched, 25, 325);
                }
        }

        public void onFinish() {
                Bot.getEventManager().removeListener(PaintListener.class, this);
        }

        public String getName() {
                return "AzWFletcher";
        }

        public String getAuthor() {
                return "AzN-WoNd3R5";
        }

        public double getVersion() {
                return 1.30;
        }

        public String getScriptCategory() {
                return "Fletching";
        }

        public String getScriptDescription() {
                String html = "";
                html += "<html>";
                html += "<center>";
                html += "<h2>" + getName() + " v" + getVersion() + " - by AzN-WoNd3R5</h2>";
                html += "<br />";
                html += "Start in one of the following banks:<br>";
                html += "Al Kharid, Draynor, Varrock, Falador, Lumbridge, Pest Control.<br>";
                html += "<br />";
                html += "<b>To string, put bows in third bank slot, and strings in forth bank slot.</b><br>";
                html += "Otherwise, <U>put the logs in your <b>THIRD</b> bank slot and have a knife in inventory.</U>";
                html += "<br />";
                html += "<br />";
                html += "<form>";
                html += "<b>Log Type: </b>";
                html += "<select name=\"log\">";
                html += "<option value=\"Normal\">Normal Logs</option>";
                html += "<option value=\"Oak\">Oak Logs</option>";
                html += "<option value=\"Willow\">Willow Logs</option>";
                html += "<option value=\"Maple\">Maple Logs</option>";
                html += "<option value=\"Yew\">Yew Logs</option>";
                html += "<option value=\"Magic\">Magic Logs</option>";
                html += "</select>";
                html += "<br>";
                html += "<br>";
                html += "<b>Fletch: </b>";
                html += "<select name=\"fletch\">";
                html += "<option value=\"Shafts\">15 Arrow Shafts</option>";
                html += "<option value=\"Shortbow\">Shortbow</option>";
                html += "<option value=\"Longbow\">Longbow</option>";
                html += "<option value=\"Stock\">Crossbow Stock</option>";
                html += "</select>";
                html += "</form>";
                html += "<br />";
                html += "<input type=\"checkbox\" name=\"string\" value=\"no\">String only?<br>";
                html += "</center>";
                html += "</html>";

                return html;
        }
}